#!/usr/bin/env python
# encoding: utf-8
"""
ttest.py

Created by Dmitry Tsybin on 2011-03-09.
Copyright (c) 2011. All rights reserved.
"""

import sys
import os
import unittest
from constants import *
from math import sqrt


class TTest:
		
	def __init__(self):
		self.longList = []
		self.shortList = []
		self.shortPop = 0
		self.longPop = 0

	
	"""подумать как быть с лонг и шорт листами"""
	def calculate(self, nowValue, sigma1 = 1, sigma2 = 2):
		t = 0
		m1 = sum(self.longList) / len(self.longList)
		m2 = sum(self.shortList) / len(self.shortList)
		n1 = len(self.longList)
		n2 = len(self.shortList)

		t = abs(m1 - m2) / sqrt( (n1 - 1) * sigma1**2 * (n2 - 1) * sigma2**2 / (n1 + n2 - 2) * (1 / n1 + 1 / n2)  )
		
		return t
		
	def discord(self, nowValue, shortTest = 10, longTest = 100):
		'''функция возвращает  STATUS'''
		if len(self.shortList) < shortTest:
			self.shortList.append(nowValue)
			self.longList.append(nowValue)
			result = STATUS.ok
		elif len(self.longList) < longTest:
			self.shortPop = self.shortList.pop(0)
			self.shortList.append(nowValue)
			self.longList.append(nowValue)
			result = STATUS.ok
		else:
			self.shortPop = self.shortList.pop(0)
			self.shortList.append(nowValue)
			self.longPop = self.longList.pop(0)
			self.longList.append(nowValue)
			
			testValue = self.calculate(nowValue)
			
			result = STATUS.crash
			"""нужно придумать, что делать со значением этого самого теста"""
			
		return result
		
class TTestTest(unittest.TestCase):
	knownValuesDiscord = [ ([0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09], STATUS.ok),
	 					([1, 2, 3, 4, 5, 6, 7, 8, 9], STATUS.ok),
						([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 1, 1, 0.1], STATUS.ok),
						([ 0 ], STATUS.ok)]						

	def testDiscord1(self):
		for listVals, status in self.knownValuesDiscord:
			testUnit = TTest()
			for nowVal in listVals:
				result = testUnit.discord(nowVal)
			self.assertEqual(status, result)

if __name__ == '__main__':
	unittest.main()	